package tk.uodrad.apex.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tk.uodrad.apex.project.pojo.dto.VehicleAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.VehicleUpdateDTO;
import tk.uodrad.apex.project.pojo.vo.VehicleListVO;
import tk.uodrad.apex.project.service.VehicleService;
import tk.uodrad.apex.project.web.JsonResult;

import java.util.Arrays;

/**
 * 说明：
 */
@RestController
@RequestMapping("/vehicle")
@Transactional
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    /**
     * 添加车辆
     * @param vehicleAddNewDTO 添加车辆DTO
     * */
    @PostMapping("/add")
    public JsonResult<String> add(@RequestBody VehicleAddNewDTO vehicleAddNewDTO) {
        vehicleService.save(vehicleAddNewDTO);
        return JsonResult.ok("添加成功");
    }

    /**
     * 根据ID数组删除车辆
     * @param ids ID数组
     * */
    @GetMapping("/delete")
    public JsonResult<String> deleteById(Long[] ids) {
        vehicleService.removeByIds(Arrays.asList(ids));
        return JsonResult.ok("删除成功！");
    }

    /**
     * 根据ID修改车辆信息
     * @param vehicleUpdateDTO 修改车辆DTO
     * */
    @PostMapping("/update")
    public JsonResult<String> update(@RequestBody VehicleUpdateDTO vehicleUpdateDTO) {
        vehicleService.updateById(vehicleUpdateDTO);
        return JsonResult.ok("修改成功！");
    }

    /**
     * 分页查询车辆列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @param communityCode 小区编号
     * @return IPage<VehicleListVO> VehicleListVO分页对象
     * */
    @GetMapping("/list")
    public JsonResult<IPage<VehicleListVO>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "communityCode", required = false) String communityCode) {
        IPage<VehicleListVO> list = vehicleService.list(pageNum, pageSize, communityCode);
        return JsonResult.ok(list);
    }
}
